//
//  YJChatInputView.m
//  YJB
//
//  Created by 中电大维 on 16/5/18.
//  Copyright © 2016年 YJB. All rights reserved.
//

#import "YJChatInputView.h"
#import "Masonry.h"
#import "YJEmotionBtn.h"
#import "YJEmotion.h"
#import "NSString+Emoji.h"
#import "AppDelegate.h"

@interface YJChatInputView ()<UITextViewDelegate>
@property(nonatomic,strong)UIImageView *inputBgimage;
@property(nonatomic,weak)UIButton *sendBtn;
@end

@implementation YJChatInputView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.inputBgimage.image = Image(@"inputBackground");
        //监听表情键盘的点击通知
        [NOTIFICATIONCENTER addObserver:self selector:@selector(insertEmotion:) name:YJEmotionButtonDidSelectNotification object:nil];
        //发送按钮点击通知
        [NOTIFICATIONCENTER addObserver:self selector:@selector(sendMessage) name:YJEmotiontabBarSendBtnDidClickNotification object:nil];
    }
    return self;
}

- (YJChatTextView *)inputTextView
{
    if (_inputTextView == nil)
    {
        _inputTextView = [[YJChatTextView alloc]init];
        _inputTextView.backgroundColor = [UIColor clearColor];
        _inputTextView.delegate = self;
        _inputTextView.returnKeyType = UIReturnKeySend;
        _inputTextView.font = FONT(17);
        _inputTextView.placeholder = @"输入内容";
        _inputTextView.placeholderColor = kUIColorFromRGB(0x999999);
        [self addSubview:_inputTextView];
        [_inputTextView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.emotionButton.mas_right);
            make.top.mas_equalTo(7);
            make.bottom.mas_equalTo(-7);
            make.right.mas_equalTo(-12);
        }];
        //上分割线
        UIView *topLineView = [[UIView alloc]init];
        topLineView.backgroundColor = [kUIColorFromRGB(0x999999) colorWithAlphaComponent:0.5];
        [self addSubview:topLineView];
        [topLineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@0);
            make.top.equalTo(@0);
            make.right.equalTo(@0);
            make.height.equalTo(@0.5);
        }];
        //下分割线
        UIView *bottomLineView = [[UIView alloc]init];
        bottomLineView.backgroundColor = [kUIColorFromRGB(0x999999) colorWithAlphaComponent:0.5];
        [self addSubview:bottomLineView];
        [bottomLineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@0);
            make.bottom.equalTo(@0);
            make.right.equalTo(@0);
            make.height.equalTo(@0.5);
        }];
    }
    return _inputTextView;
}

//-(UIButton *)sendBtn
//{
//    if (_sendBtn == nil)
//    {
//      UIButton *sendBtn = [[UIButton alloc]init];
//        [sendBtn setBackgroundImage:Image(@"chat_sendButton") forState:UIControlStateNormal];
//        sendBtn.titleLabel.font = FONT(15);
//        [sendBtn addTarget:self action:@selector(sendMessage) forControlEvents:UIControlEventTouchUpInside];
//        [sendBtn setBackgroundImage:Image(@"chat_sendButton_disable") forState:UIControlStateDisabled];
//        [sendBtn setTitle:@"发送" forState:UIControlStateNormal];
//        sendBtn.enabled = NO;
//        [self addSubview:sendBtn];
//        _sendBtn = sendBtn;
//        [_sendBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.right.mas_equalTo(-10);
//            make.bottom.mas_equalTo(-7);
//            make.height.mas_equalTo(36);
//            make.width.mas_equalTo(55);
//        }];
//    }
//    return _sendBtn;
//}

//输入背景框
- (UIImageView *)inputBgimage
{
    if (_inputBgimage == nil)
    {
        _inputBgimage = [[UIImageView alloc]init];
        [self addSubview:_inputBgimage];
        [_inputBgimage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.inputTextView.mas_left);
            make.top.mas_equalTo(self.inputTextView.mas_top);
            make.bottom.mas_equalTo(self.inputTextView.mas_bottom);
            make.right.mas_equalTo(self.inputTextView.mas_right);
        }];
    }
    return _inputBgimage;
}

//更多按钮
- (UIButton *)moreItem
{
    if (_moreItem == nil)
    {
        _moreItem = [[UIButton alloc]init];
        [_moreItem setImage:Image(@"chat_add") forState:UIControlStateNormal];
        [_moreItem setImage:Image(@"chat_add_click") forState:UIControlStateSelected];
        [_moreItem addTarget:self action:@selector(addClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_moreItem];
        [_moreItem mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.bottom.mas_equalTo(0);
            make.width.height.mas_equalTo(50);
        }];
    }
    return _moreItem;
}

- (UIButton *)emotionButton
{
    if (_emotionButton == nil)
    {
        _emotionButton = [[UIButton alloc]init];
        [_emotionButton setImage:Image(@"emotion") forState:UIControlStateNormal];
        [_emotionButton setImage:Image(@"emotion_select") forState:UIControlStateSelected];
        [_emotionButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
        [_emotionButton addTarget:self action:@selector(emotionClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_emotionButton];
        [_emotionButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(0);
            make.left.mas_equalTo(self.moreItem.mas_right);
            make.width.mas_equalTo(38);
            make.height.mas_equalTo(50);
        }];
    }
    return _emotionButton;
}

- (void)emotionClick:(UIButton *)button
{
    if (self.toolBarClickBlock)
    {
        self.toolBarClickBlock(YJChatInputViewButtonTypeEmotion);
    }
}

- (void)addClick:(UIButton *)button
{
    if (self.toolBarClickBlock)
    {
        self.toolBarClickBlock(YJChatInputViewButtonTypeAdd);
    }
}

- (void)sendMessage
{
    AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    if (appDelegate.isConnectWebsocket == NO)
    {
        [MBProgressHUD showError:@"网络未连接,稍后重试"];
        return;
    }
    if (self.sendMessageBlock)
    {
        self.sendMessageBlock(self.inputTextView.text);
        self.inputTextView.text = nil;
        self.sendBtn.enabled = NO;
    }
}

- (void)insertEmotion:(NSNotification *)noti
{
    NSDictionary *dict = noti.userInfo;
    YJEmotionBtn *btn = dict[@"keyBoardBtn"];
    if (btn.tag == 20)//点击的是删除按钮
    {
        [self.inputTextView deleteBackward];
    }else if(btn.emotion.code.length != 0)//点击的是emoji表情
    {
       [self.inputTextView insertText:btn.emotion.code.emoji];
    }else//点击的是自定义表情
    {
        //拿出表情对应的文字描述
        NSString *chs = btn.emotion.chs;
        //插入文字
        [self.inputTextView insertText:chs];
        [self.inputTextView scrollRangeToVisible:self.inputTextView.selectedRange];
    }
}


-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"])
    {
        AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
        if (appDelegate.isConnectWebsocket == NO)
        {
            [MBProgressHUD showError:@"网络未连接,不能发消息"];
            return NO;
        }
        self.inputTextView.placeholderHiden = NO;
        if (self.sendMessageBlock)
        {
            self.sendMessageBlock(textView.text);
            textView.text = @"";
            self.sendBtn.enabled = NO;
        }
        return NO;
    }
    if (_isGroupChat && [text isEqualToString:@"@"])
    {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
           [NOTIFICATIONCENTER postNotificationName:YJUserDidInputSpecialNotification object:nil];
        });
    }
    return YES;
}

-(void)textViewDidChange:(UITextView *)textView
{
    self.sendBtn.enabled = textView.hasText;
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    
}

- (void)dealloc
{
    [NOTIFICATIONCENTER removeObserver:self];
}

@end
